import { TaskName } from '@/views/page-config/flow/flow-modeler';
import { FormFieldModel, FormModel, FormTypeEnum } from 'pf-component';
import {
  definitionType,
  eventType,
  fieldType,
  listenerType
} from '@/views/page-config/flow/custom-properties/listener-utils';

export const buildBaseInfo = (type: string): FormModel => {
  const baseFields = [
    new FormFieldModel({
      prop: 'id',
      label: 'ID',
      type: FormTypeEnum.INPUT.code,
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'name',
      label: '名称',
      spanCol: 2
    })
  ];

  if (TaskName.process === type) {
    const fields = [
      new FormFieldModel({
        prop: 'versionTag',
        label: '版本标签',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'isExecutable',
        label: '可执行',
        type: FormTypeEnum.SWITCH.code,
        activeVal: true,
        inactiveVal: false,
        spanCol: 2
      })
    ];
    return new FormModel({
      name: 'flowBaseInfoForm',
      fields: [...baseFields, ...fields]
    });
  } else if (TaskName.subProcess === type) {
    const fields = [
      new FormFieldModel({
        prop: 'isExpanded',
        label: '状态',
        type: FormTypeEnum.SWITCH.code,
        activeVal: true,
        inactiveVal: false,
        spanCol: 2
      })
    ];
    return new FormModel({
      name: 'flowBaseInfoForm',
      fields: [...baseFields, ...fields]
    });
  } else {
    return new FormModel({
      name: 'flowBaseInfoForm',
      fields: [...baseFields]
    });
  }
};

export const buildBizFormInfo = (): FormModel => {
  return new FormModel({
    name: 'bizFormInfo',
    fields: [
      new FormFieldModel({
        prop: 'formKey',
        label: '表单名称',
        spanCol: 2,
        disable: true,
        required: false,
        append: 'formKey-slot'
      })
    ]
  });
};

export const buildSignalInfo = (): FormModel => {
  return new FormModel({
    name: 'signalForm',
    fields: [
      new FormFieldModel({
        prop: 'id',
        label: '信号ID',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'name',
        label: '信号名称',
        spanCol: 2
      })
    ]
  });
};

export const buildMessageInfo = (): FormModel => {
  return new FormModel({
    name: 'messageForm',
    fields: [
      new FormFieldModel({
        prop: 'id',
        label: '消息ID',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'name',
        label: '消息名称',
        spanCol: 2
      })
    ]
  });
};

export const buildFlowConditionInfo = (): FormModel => {
  return new FormModel({
    name: 'flowConditionForm',
    fields: [
      new FormFieldModel({
        prop: 'type',
        label: '流转类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'normal', value: '普通流转路径' },
          { key: 'default', value: '默认流转路径' },
          { key: 'condition', value: '条件流转路径' }
        ]
      }),
      new FormFieldModel({
        prop: 'conditionType',
        label: '条件格式',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'expression', value: '表达式' },
          { key: 'script', value: '脚本' }
        ]
      }),
      new FormFieldModel({
        prop: 'language',
        label: '脚本语言',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'scriptType',
        label: '脚本类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'inlineScript', value: '内联脚本' },
          { key: 'externalScript', value: '外部脚本' }
        ]
      }),
      new FormFieldModel({
        prop: 'body',
        label: '表达式' /*脚本*/,
        spanCol: 2
      }),
      /*new FormFieldModel({
        prop: "body",
        label: "脚本",
        spanCol: 2,
        type: FormTypeEnum.TEXTAREA.code
      }),*/
      new FormFieldModel({
        prop: 'resource',
        label: '资源地址',
        spanCol: 2
      })
    ]
  });
};

export const buildReceiveTaskInfo = (): FormModel => {
  return buildTaskInfo([
    new FormFieldModel({
      prop: 'messageId',
      label: '消息实例',
      spanCol: 2,
      type: FormTypeEnum.SELECT.code,
      options: []
    })
  ]);
};
export const buildUserTaskInfo = (): FormModel => {
  const userFormFields = [
    new FormFieldModel({
      prop: 'assignee',
      label: '处理用户',
      spanCol: 2,
      disable: true,
      append: 'assignee-slot'
    }),
    new FormFieldModel({
      prop: 'candidateUsers',
      label: '候选用户',
      spanCol: 2,
      disable: true,
      append: 'candidateUsers-slot'
    }),
    new FormFieldModel({
      prop: 'candidateGroups',
      label: '候选分组',
      spanCol: 2,
      disable: true,
      append: 'candidateGroups-slot'
    }),
    new FormFieldModel({
      prop: 'dueDate',
      label: '到期时间',
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'followUpDate',
      label: '跟踪时间',
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'priority',
      label: '优先级',
      spanCol: 2
    })
  ];
  return buildTaskInfo(userFormFields);
};
export const buildScriptTaskInfo = (): FormModel => {
  const fields = [
    new FormFieldModel({
      prop: 'scriptFormat',
      label: '脚本格式',
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'scriptType',
      label: '脚本类型',
      spanCol: 2,
      type: FormTypeEnum.SELECT.code,
      options: [
        { key: 'inlineScript', value: '内联脚本' },
        { key: 'externalScript', value: '外部脚本' }
      ]
    }),
    new FormFieldModel({
      prop: 'script',
      label: '脚本',
      spanCol: 2,
      type: FormTypeEnum.TEXTAREA.code
    }),
    new FormFieldModel({
      prop: 'resource',
      label: '资源地址',
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'resultVariable',
      label: '结果变量',
      spanCol: 2
    })
  ];
  return buildTaskInfo(fields);
};
export const buildTaskInfo = (fields?: FormFieldModel[]): FormModel => {
  const _fields: FormFieldModel[] = fields || [];
  return new FormModel({
    name: 'taskForm',
    fields: [
      new FormFieldModel({
        prop: '_async',
        label: '异步延续',
        spanCol: 2,
        value: [],
        type: FormTypeEnum.CHECKBOX.code,
        options: [
          { key: 'asyncBefore', value: '异步前' },
          { key: 'asyncAfter', value: '异步后' },
          { key: 'exclusive', value: '排除' }
        ]
      }),
      ..._fields
    ]
  });
};

export const buildMultiInstance = (): FormModel => {
  return new FormModel({
    name: 'multiInstanceForm',
    fields: [
      new FormFieldModel({
        prop: 'loopCharacteristics',
        label: '回路特性',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'ParallelMultiInstance', value: '并行多重事件' },
          { key: 'SequentialMultiInstance', value: '时序多重事件' },
          { key: 'StandardLoop', value: '循环事件' },
          { key: '-1', value: '无' }
        ]
      }),
      new FormFieldModel({
        prop: 'loopCardinality',
        label: '循环基数',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'collection',
        label: '集合',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'elementVariable',
        label: '元素变量',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'completionCondition',
        label: '完成条件',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'timeCycle',
        label: '重试周期',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: '_async',
        label: '异步状态',
        spanCol: 2,
        type: FormTypeEnum.CHECKBOX.code,
        value: [],
        options: [
          { key: 'asyncBefore', value: '异步前' },
          { key: 'asyncAfter', value: '异步后' },
          { key: 'exclusive', value: '排除' }
        ]
      })
    ]
  });
};

export const buildExTaskListener = (): FormModel => {
  const eventTypes = Object.keys(eventType).map(k => {
    return { key: k, value: eventType[k] };
  });
  const definitionTypes = Object.keys(definitionType).map(k => {
    return { key: k, value: definitionType[k] };
  });
  const form = buildExListener([
    new FormFieldModel({
      prop: 'id',
      label: '监听器ID',
      spanCol: 2
    }),
    new FormFieldModel({
      prop: 'eventDefinitionType',
      label: '定时器类型',
      spanCol: 2,
      type: FormTypeEnum.SELECT.code,
      options: definitionTypes
    }),
    new FormFieldModel({
      prop: 'eventTimeDefinitions',
      label: '定时器',
      spanCol: 2
    })
  ]);

  const eventField = form.fields.find(f => f.prop === 'event');
  if (eventField) eventField.options = eventTypes;

  return form;
};
export const buildExListener = (fields?: FormFieldModel[]): FormModel => {
  const _fields = fields || [];
  const listenerTypes = Object.keys(listenerType).map(k => {
    return { key: k, value: listenerType[k] };
  });
  return new FormModel({
    name: 'exListenerForm',
    fields: [
      new FormFieldModel({
        prop: 'event',
        label: '事件类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'start', value: 'start' },
          { key: 'end', value: 'end' }
        ]
      }),
      new FormFieldModel({
        prop: 'listenerType',
        label: '监听器类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: listenerTypes
      }),
      new FormFieldModel({
        prop: 'class',
        label: 'Java类',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'expression',
        label: '表达式',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'delegateExpression',
        label: '代理表达式',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'scriptFormat',
        label: '脚本格式',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'scriptType',
        label: '脚本类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: [
          { key: 'inlineScript', value: '内联脚本' },
          { key: 'externalScript', value: '外部脚本' }
        ]
      }),
      new FormFieldModel({
        prop: 'value',
        label: '脚本内容',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'resource',
        label: '资源地址',
        spanCol: 2
      }),
      ..._fields
    ]
  });
};
export const buildExListenerField = (): FormModel => {
  const listenerFieldTypes = Object.keys(fieldType).map(k => {
    return { key: k, value: fieldType[k] };
  });
  return new FormModel({
    name: 'taskForm',
    fields: [
      new FormFieldModel({
        prop: 'name',
        label: '字段名称',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'fieldType',
        label: '字段类型',
        spanCol: 2,
        type: FormTypeEnum.SELECT.code,
        options: listenerFieldTypes
      }),
      new FormFieldModel({
        prop: 'string',
        label: '字段值',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'expression',
        label: '表达式',
        spanCol: 2
      })
    ]
  });
};

export const buildPropertiesInfo = (): FormModel => {
  return new FormModel({
    name: 'flowPropertiesForm',
    fields: [
      new FormFieldModel({
        prop: 'name',
        label: '属性名',
        spanCol: 2
      }),
      new FormFieldModel({
        prop: 'value',
        label: '属性值',
        spanCol: 2
      })
    ]
  });
};
